package edu.ncu.interceptor;

import edu.ncu.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("进入了登录拦截器...");
        //看看session里面有没有user对应的属性
//        System.out.println(request.getSession());
        User user = (User)request.getSession().getAttribute("user");
        //如果没有，说明没有登陆，就重定向到登陆页面
        if(user == null)
        {
            System.out.println("没有登录，重定向地址："+request.getContextPath()+"/user/login");
            response.sendRedirect(request.getContextPath()+"/login");  //绝对地址
            //相对地址是不加/，然后和http://localhost:8080/makefriend/photo拼接
            return false;
        }
        System.out.println("已登录，account为：" + user.getAccount());
        return true;
    }
}
